// 文件路径: src/main/java/com/chixing/controller/UserCouponController.java

package com.chixing.controller;

import com.chixing.entity.UserCoupon;
import com.chixing.service.IUserCouponService;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户优惠券前端控制器
 * </p>
 *
 * @author liwenjing
 * @since 2025-10-07
 */
@RestController
@RequestMapping("/userCoupon")
public class UserCouponController {

    @Autowired
    private IUserCouponService userCouponService;

    /**
     * 获取用户所有优惠券
     * @param userId 用户ID
     * @return 优惠券列表
     */
    @GetMapping("/user/{userId}")
    public Result getUserCoupons(@PathVariable Integer userId) {
        try {
            List<UserCoupon> userCoupons = userCouponService.getUserCouponsByUserId(userId);
            return Result.getSuccess(userCoupons);
        } catch (Exception e) {
            return Result.error("获取用户优惠券失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取用户优惠券详情
     * @param userCouponId 用户优惠券ID
     * @return 用户优惠券信息
     */
    @GetMapping("/{userCouponId}")
    public Result getUserCoupon(@PathVariable Integer userCouponId) {
        try {
            UserCoupon userCoupon = userCouponService.getUserCouponById(userCouponId);
            if (userCoupon != null) {
                return Result.getSuccess(userCoupon);
            } else {
                return Result.error("未找到指定的用户优惠券");
            }
        } catch (Exception e) {
            return Result.error("获取用户优惠券详情失败: " + e.getMessage());
        }
    }

    /**
     * 更新用户优惠券
     * @param userCouponId 用户优惠券ID
     * @param userCoupon 用户优惠券对象
     * @return 更新结果
     */
    @PutMapping("/{userCouponId}")
    public Result updateUserCoupon(@PathVariable Integer userCouponId, @RequestBody UserCoupon userCoupon) {
        try {
            userCoupon.setUserCouponId(userCouponId);
            userCoupon.setUpdateTime(LocalDateTime.now());
            boolean result = userCouponService.updateUserCoupon(userCoupon);
            if (result) {
                return Result.updateSuccess("更新用户优惠券成功");
            } else {
                return Result.error("更新用户优惠券失败");
            }
        } catch (Exception e) {
            return Result.error("更新用户优惠券异常: " + e.getMessage());
        }
    }

    /**
     * 删除用户优惠券
     * @param userCouponId 用户优惠券ID
     * @return 删除结果
     */
    @DeleteMapping("/{userCouponId}")
    public Result deleteUserCoupon(@PathVariable Integer userCouponId) {
        try {
            boolean result = userCouponService.deleteUserCoupon(userCouponId);
            if (result) {
                return Result.deleteSuccess("删除用户优惠券成功");
            } else {
                return Result.error("删除用户优惠券失败");
            }
        } catch (Exception e) {
            return Result.error("删除用户优惠券异常: " + e.getMessage());
        }
    }
    @PostMapping
    public Result receiveCoupon(
            @RequestBody UserCoupon userCoupon) {
        System.out.println(userCoupon);
        return userCouponService.receiveCoupon(userCoupon);
    }

    @GetMapping("userId/{userId}/status/{mycouponStatus}")
    public Result getMyAllCoupons(@PathVariable("userId") Integer userId,
                                  @PathVariable("mycouponStatus") Integer mycouponStatus) {
        return userCouponService.getMyAllCoupons(userId, mycouponStatus);
    }
}

